package com.lang.progress;

import com.lang.downloader.http.config.HttpDownloadConfig;
import com.lang.util.DownloadConfigManager;

import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

public class HttpConfigDWhenCount implements DWhenCount {

    /**
     * 下载配置信息
     */
    private final AtomicReference<HttpDownloadConfig> configAtomicReference;

    public HttpConfigDWhenCount(AtomicReference<HttpDownloadConfig> configAtomicReference) {
        this.configAtomicReference = configAtomicReference;
    }

    /**
     * 下载计数
     *
     * @param name     名称
     * @param count    本次计数
     * @param hasDone  已完成数
     * @param fileSize 文件长度
     * @param done     是否已完成
     */
    @Override
    public void whenCount(String name, Long count, AtomicLong hasDone, Long fileSize, Boolean done) {
        this.configAtomicReference.updateAndGet(httpDownloadConfig -> {
            httpDownloadConfig.setContentLength(fileSize);
            httpDownloadConfig.setHasDone(hasDone);
            httpDownloadConfig.setDone(done);
            DownloadConfigManager.writeDownloadConfig(httpDownloadConfig.getConfigFilePath(), httpDownloadConfig);
            return httpDownloadConfig;
        });
    }
}
